/*
 * Copyright (c) 2019 Actions Semiconductor Co., Ltd
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @file
 * @brief audio_input event
 */

#include "audio_input.h"
#include "tts_manager.h"

#ifdef CONFIG_ESD_MANAGER
#include "esd_manager.h"
#endif

void audio_input_tts_event_proc(struct app_msg *msg)
{
	struct audio_input_app_t *audio_input = audio_input_get_app();

	if (!audio_input)
		return;
	SYS_LOG_INF("tts status:%d\n",msg->value);
	switch (msg->value) {
	case TTS_EVENT_START_PLAY:
		break;
		if (audio_input->player) {
			audio_input->need_resume_play = 1;
			audio_input_stop_play();
		}
		break;
	case TTS_EVENT_STOP_PLAY:
		if(key_tone_play_flag)
		{
			key_tone_play_flag=0;
			tts_restor_aux_play_status();
			break;
		}
		if (audio_input->need_resume_play && !audio_input->playing) {
			if (thread_timer_is_running(&audio_input->monitor_timer)) {
				thread_timer_stop(&audio_input->monitor_timer);
			}
			thread_timer_init(&audio_input->monitor_timer, audio_input_delay_resume, NULL);
			thread_timer_start(&audio_input->monitor_timer, 2000, 0);
		}
		
		
		break;
	default:
		break;
	}
}
u32_t audio_input_get_audio_stream_type(char *app_name)
{
	if (memcmp(app_name, APP_ID_LINEIN, strlen(APP_ID_LINEIN)) == 0)
		return AUDIO_STREAM_LINEIN;
	else if (memcmp(app_name, APP_ID_SPDIF_IN, strlen(APP_ID_SPDIF_IN)) == 0)
		return AUDIO_STREAM_SPDIF_IN;
	else if (memcmp(app_name, APP_ID_MIC_IN, strlen(APP_ID_MIC_IN)) == 0)
		return AUDIO_STREAM_MIC_IN;
	else if (memcmp(app_name, APP_ID_I2SRX_IN, strlen(APP_ID_I2SRX_IN)) == 0)
		return AUDIO_STREAM_I2SRX_IN;

	SYS_LOG_WRN("%s unsupport\n", app_name);
	return 0;
}
void audio_input_sync_event_proc(struct app_msg *msg)
{
	u8_t sync_vol;
	
	SYS_LOG_INF("cmd:%d->wait_tts_finish:%d\n",msg->cmd,wait_tts_finish);
	switch (msg->cmd)
	{
		case CMD_KEY_VOL_ADD:
			sync_vol=msg->content[0];
			if(wait_tts_finish)
			{
				volume_bakup=sync_vol;
				audio_system_set_linein_volume(volume_bakup);
			}
			else
			{
				audio_aux_volume_up(1, 0);
			}
			
			if(sync_vol!=system_volume_get(AUDIO_STREAM_LINEIN))
			{
				if(!wait_tts_finish)
				{
					system_volume_set(AUDIO_STREAM_LINEIN,sync_vol,0);
				}				
				if(system_volume_get(AUDIO_STREAM_LINEIN)==0)
				{
					set_key_lights(KEY_LIGHT_VOLSUB,RGB_RED);
				}
				else
				{
					struct uart_msg_info remote_info;
					sys_get_remote_info(&remote_info);
					SYS_LOG_INF("ap:%d->mute:%d\n",remote_info.ap_mode,remote_info.usb_mute);
					if(remote_info.ap_mode==0&&remote_info.usb_mute)
					{
						set_key_lights(KEY_LIGHT_VOLSUB,RGB_RED);
					}
					else
					{
						set_key_lights(KEY_LIGHT_VOLSUB,RGB_WHITE);
					}
					//set_key_lights(KEY_LIGHT_VOLSUB,RGB_WHITE);
				}
			}
			volume_bakup=system_volume_get(AUDIO_STREAM_LINEIN);
			break;
		case CMD_KEY_VOL_SUB:
			sync_vol=msg->content[0];
			if(wait_tts_finish)
			{
				volume_bakup=sync_vol;
				audio_system_set_linein_volume(volume_bakup);
			}
			else
			{
				audio_aux_volume_down(1, 0);
			}			
		    if(sync_vol==0)
		    {
		    	SYS_LOG_INF("sync_vol:%d->cur vol:%d\n",sync_vol,system_volume_get(AUDIO_STREAM_LINEIN));
				set_key_lights(KEY_LIGHT_VOLSUB,RGB_RED);
		    }
			if(sync_vol!=system_volume_get(AUDIO_STREAM_LINEIN))
			{
				if(!wait_tts_finish)
				{
					system_volume_set(AUDIO_STREAM_LINEIN,sync_vol,0);
				}				
				if(system_volume_get(AUDIO_STREAM_LINEIN)==0)
				{
					set_key_lights(KEY_LIGHT_VOLSUB,RGB_RED);
				}
				else
				{
					struct uart_msg_info remote_info;
					sys_get_remote_info(&remote_info);
					if(remote_info.ap_mode==0&&remote_info.usb_mute)
					{
						set_key_lights(KEY_LIGHT_VOLSUB,RGB_RED);
					}
					else
					{
						set_key_lights(KEY_LIGHT_VOLSUB,RGB_WHITE);
					}
				}
			}
			volume_bakup=system_volume_get(AUDIO_STREAM_LINEIN);
			break;
		default:
			break;
			
	}
}
void audio_input_bat_check_timer(struct thread_timer *ttimer, void *expiry_fn_arg)
{
	SYS_LOG_INF("timeout!!\n");
	if(system_get_mic_status())
	{
		set_corner_lights(RGB_RED);
	}
	else
	{
		struct uart_msg_info remote_info;
		u8_t set_rgb;
		sys_get_remote_info(&remote_info);
		if(remote_info.ap_mode==0)
		{
			set_rgb=RGB_GREEN;
		}
		else
		{
			set_rgb=RGB_BLUE;
		}
		if(get_bat_status())
		{
			set_corner_lights(set_rgb);
		}
		else
		{
			
			if (sys_pm_get_power_5v_status())
			{
				set_breath_lights(set_rgb);
			}
			else
			{
				set_corner_lights(set_rgb);
			}
			
		}
	}
	
}

void audio_input_input_event_proc(struct app_msg *msg)
{
	struct audio_input_app_t *audio_input = audio_input_get_app();
	u8_t sync_data[3]={0};
	static u32_t audio_timestampe = 0;
	if (!audio_input)
		return;
	SYS_LOG_INF("msg->cmd:%d\n",msg->cmd);
	switch (msg->cmd) {
	case MSG_AUDIO_TTS_END:
		k_sleep(10);
		audio_adda_channel_test();
		break;
	case MSG_AUDIO_INPUT_PLAY_PAUSE_RESUME:
		#if 1
		if(power_manager_get_battery_capacity()==0)
		{
			break;
		}
		if(get_breath_mode())
		{
			disable_breath_lights();
		}
		sys_show_battery_capacity();
		if (thread_timer_is_running(&audio_input->bat_check_timer))
		{
			thread_timer_stop(&audio_input->bat_check_timer);
		}		
		thread_timer_init(&audio_input->bat_check_timer, audio_input_bat_check_timer, NULL);
		thread_timer_start(&audio_input->bat_check_timer, 2000, 0);
		#else
		sync_data[0]=0;
		system_set_sync_data(sync_data);
		system_sync_to_remote_start(ACK_OUT,CMD_SYNC_KEY_ONOFF);
		#endif
		break;
	#if 0
		#ifdef LINEIN_ADDA_MODE
		audio_aux_play_ctl();
		#else
		if (audio_input->playing) {
			audio_input_stop_play();
			sys_event_notify(SYS_EVENT_PLAY_PAUSE);
		} else {
			sys_event_notify(SYS_EVENT_PLAY_START);
			if (thread_timer_is_running(&audio_input->monitor_timer)) {
				thread_timer_stop(&audio_input->monitor_timer);
			}
			thread_timer_init(&audio_input->monitor_timer, audio_input_delay_resume, NULL);
			thread_timer_start(&audio_input->monitor_timer, 200, 0);
			audio_input->playing = 1;
		}
		audio_input_store_play_state();
	#ifdef CONFIG_ESD_MANAGER
		{
			u8_t playing = audio_input->playing;

			esd_manager_save_scene(TAG_PLAY_STATE, &playing, 1);
		}
	#endif
	#endif
		break;
	#endif
	case MSG_AUDIO_INPUT_PLAY_VOLUP:
		if(wait_tts_finish)
		{
			break;
		}
		if ((u32_t)(k_cycle_get_32() - audio_timestampe) / 24 < 100000)
		{
			break;
		}
		audio_timestampe = k_cycle_get_32();
		#ifdef LINEIN_ADDA_MODE
		if(systen_get_cascade_status())
		{
			audio_aux_volume_up(1,0);
		}
		else
		{
			audio_aux_volume_up(1,1);
		}
		volume_bakup=system_volume_get(AUDIO_STREAM_LINEIN);
		#else
	 	system_volume_up(audio_input_get_audio_stream_type(app_manager_get_current_app()), 1);
		#endif		
		system_sync_to_remote_start(ACK_OUT,CMD_SYNC_KEY_VOL_ADD);
		break;
	case MSG_AUDIO_INPUT_PLAY_VOLDOWN:
		if(wait_tts_finish)
		{
			break;
		}
		if ((u32_t)(k_cycle_get_32() - audio_timestampe) / 24 < 100000)
		{
			break;
		}
		audio_timestampe = k_cycle_get_32();
		#ifdef LINEIN_ADDA_MODE
		if(systen_get_cascade_status())
		{
			audio_aux_volume_down(1,0);
		}
		else
		{
			audio_aux_volume_down(1,1);
		}
		volume_bakup=system_volume_get(AUDIO_STREAM_LINEIN);
		#else
		system_volume_down(audio_input_get_audio_stream_type(app_manager_get_current_app()), 1);
		#endif
		system_sync_to_remote_start(ACK_OUT,CMD_SYNC_KEY_VOL_SUB);
		break;
	case MSG_AUDIO_INPUT_SAMPLE_RATE_CHANGE:
		audio_input_stop_play();
		audio_input_start_play();
		break;
	case MSG_AUDIO_INPUT_HUNG_SHORT:
		sync_data[0]=0;
		system_set_sync_data(sync_data);
		system_sync_to_remote_start(ACK_OUT,CMD_SYNC_KEY_HUNG);
		break;
	case MSG_AUDIO_INPUT_HUNG_LONG:
		sync_data[0]=1;
		system_set_sync_data(sync_data);
		system_sync_to_remote_start(ACK_OUT,CMD_SYNC_KEY_HUNG);
		break;
	#ifdef LINEIN_ADDA_MODE
	case MSG_KEY_POWER_OFF:
		audio_adc_stop();
		sys_event_notify(SYS_EVENT_POWER_OFF);
		break;
	#endif
	default:
		break;
	}
}

void audio_input_bt_event_proc(struct app_msg *msg)
{
	if (msg->cmd == BT_REQ_RESTART_PLAY) {
		audio_input_stop_play();
		os_sleep(200);
		SYS_LOG_INF("111111\n");
		audio_input_start_play();
	}
	if (msg->cmd == BT_TWS_DISCONNECTION_EVENT) {
		struct audio_input_app_t *audio_input = audio_input_get_app();
		if (audio_input->player) {
			media_player_set_output_mode(audio_input->player, MEDIA_OUTPUT_MODE_DEFAULT);
		}
	}
#ifndef CONFIG_TWS_BACKGROUND_BT
	if (msg->cmd == BT_TWS_CONNECTION_EVENT) {
		bt_manager_halt_phone();
	} else if (msg->cmd == BT_TWS_DISCONNECTION_EVENT) {
		bt_manager_resume_phone();
	}
#endif
}



